//
//  MainHomeOrderViewCell.m
//  ERP
//
//  Created by QA on 16/6/3.
//  Copyright © 2016年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "MainHomeOrderViewCell.h"
#import "WebServiceCommonRequest.h"
#import "NSDictionary+DataFormat.h"
#import "AbstractBaseViewController.h"

#import "WebViewController.h"
#import "GEPushViewControllerHelper.h"
#import "WWTBtnView.h"
#import "UIColorDispose.h"
#import "QueryMessageService.h"
#import "JGProgressHUDHelper.h"

#define CellHeight 72

@interface MainHomeOrderViewCell ()

@property (nonatomic, strong) UINavigationController *navigationController;
@property (nonatomic, strong) NSArray *menuValueArray;

@property (nonatomic, strong) IBOutlet UIButton *createButton;
@property (nonatomic, strong) IBOutlet UIButton *orderInfoButton;

@end


@implementation MainHomeOrderViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
//        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MainHomeOrderViewCell" owner:self options:nil];
//        self = [topLevelObjects objectAtIndex:0];
    }
    return self;
}


-(void)setMenuArray:(NSArray *)menuArray navigation:(UINavigationController*)navigation
{
    self.navigationController = navigation;
    self.menuValueArray = menuArray;
    
    UIView *_backView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, CellHeight)];
    [self addSubview:_backView1];
    
    //创建2个
    for (int i = 0; i < menuArray.count; i++) {
        if (i < 2) {
            ModuleAuthorityEntity *entity = [menuArray objectAtIndex:i];
            
            WWTBtnView *btnView = [[WWTBtnView alloc] initWithLeftImgRightTextFrame:CGRectMake(i*SuperWidth/2, 0, SuperWidth/2, CellHeight)];
            [btnView setTitle:entity.title detail:entity.descripter imageStr:entity.icon];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OnTapBtnView:)];
            [btnView addGestureRecognizer:tap];
            
            btnView.tag = i;
            btnView.titleLable.font = [UIFont systemFontOfSize:15];
            btnView.titleLable.textColor = [UIColorDispose colorWithHexString:@"#333333"];
            [_backView1 addSubview:btnView];
        }
    }
    
}

-(void)OnTapBtnView:(UITapGestureRecognizer *)sender
{
    NSLog(@"tag:%ld",sender.view.tag);
    
    ModuleAuthorityEntity *entity = [self.menuValueArray objectAtIndex:sender.view.tag];
    ModuleAuthorityEntity *authorityEntity = [self getModuleAuthorityEntity:entity.id];
    if(!authorityEntity){
        [JGProgressHUDHelper showErrorHUDText:@"无权限"];
        return;
    }
    
    if(authorityEntity.codeType.integerValue == 1){
        //网页模式
        WebPushParameterModel *model = [[WebPushParameterModel alloc] init];
        model.URL = authorityEntity.webCode;
        model.Title = authorityEntity.title;
        model.CodeType = CodeTypeWeb;
        
        WebViewController *viewController = [[WebViewController alloc] initWithPushParameterModel:model];
        [self.navigationController pushViewController:viewController animated:YES];
    } else {
        //原生APP模式
        [GEPushViewControllerHelper pushViewControllerHomePageCode:authorityEntity.nativeCode navigation:self.navigationController];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

//获取菜单权限信息
- (ModuleAuthorityEntity*)getModuleAuthorityEntity:(id)idValue
{
    NSPredicate *menuPredicat = [NSPredicate predicateWithFormat:@"id = %@", idValue];
    NSArray *menuArray = [ModuleAuthorityEntity MR_findAllSortedBy:@"order" ascending:YES withPredicate:menuPredicat];
    if(menuArray.count <=0){
        return nil;
    } else {
        return menuArray.firstObject;
    }
}

@end
